package com.aries.crossgate.common.job.service.impl;

import com.aries.crossgate.common.job.dao.QuartzJobMapper;
import com.aries.crossgate.common.job.entity.QuartzJobEntity;
import com.aries.crossgate.common.job.service.QuartzJobService;
import com.aries.crossgate.common.job.utils.ScheduleUtils;
import com.aries.crossgate.common.utils.Constant;
import com.aries.crossgate.common.utils.R;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.apache.commons.lang.StringUtils;
import org.quartz.CronTrigger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.List;

/**
 * 定时任务
 *
 * @author ZhouChenglin
 * @email yczclcn@163.com
 * @url www.chenlintech.com
 * @date 2017年8月20日 下午11:49:18
 */
@Service("quartzJobService")
public class QuartzJobServiceImpl implements QuartzJobService {
	
	@Autowired
	private QuartzJobMapper quartzJobManager;
	
	/**
	 * 项目启动，初始化任务
	 */
	/*@PostConstruct
	public void init() {
		EntityWrapper entityWrapper = new EntityWrapper();

		List<QuartzJobEntity> jobList = quartzJobManager.selectList(entityWrapper);
		for(QuartzJobEntity job : jobList) {
			CronTrigger cronTrigger = ScheduleUtils.getCronTrigger(job.getJobId());
            //如果不存在，则创建
            if(cronTrigger == null) {
                ScheduleUtils.createScheduleJob(job);
            }else {
                ScheduleUtils.updateScheduleJob(job);
            }
		}
	}*/

	@Override
	public List<QuartzJobEntity> getJob(QuartzJobEntity quartzJobEntity) {
		EntityWrapper entityWrapper = new EntityWrapper();
		if(StringUtils.isNotEmpty(quartzJobEntity.getBeanName()))
			entityWrapper.like("bean_name",quartzJobEntity.getBeanName());
		if(StringUtils.isNotEmpty(quartzJobEntity.getRemark()))
			entityWrapper.like("remark",quartzJobEntity.getRemark());
		List<QuartzJobEntity> jobList = quartzJobManager.selectList(entityWrapper);
		return jobList;
	}

	@Override
	public R save(QuartzJobEntity job) {
		job.setStatus(Constant.ScheduleStatus.NORMAL.getValue());
		int count = quartzJobManager.insert(job);
		ScheduleUtils.createScheduleJob(job);
		return R.ok(Constant.SUCCESS);
	}

	@Override
	public QuartzJobEntity getQuartzJobById(String jobId) {
		QuartzJobEntity job = quartzJobManager.selectById(jobId);
		return job;
	}

	@Override
	public R updateQuartzJob(QuartzJobEntity job) {
		int count = quartzJobManager.updateById(job);
		ScheduleUtils.updateScheduleJob(job);
		return R.ok(Constant.SUCCESS);
	}

	@Override
	public R batchRemoveQuartzJob(String[] ids) {
		for(String jobId : ids) {
			ScheduleUtils.deleteScheduleJob(jobId);
		}
		int count = quartzJobManager.deleteBatchIds(Arrays.asList(ids));
		return R.ok(Constant.SUCCESS);
	}
	
	@Override
	public R run(String[] id) {
		for(String jobId : id) {
			ScheduleUtils.run(quartzJobManager.selectById(jobId));
		}
		return R.ok(Constant.SUCCESS);
	}
	
	@Override
	public R pause(String[] id) {
		for(String jobId : id) {
			ScheduleUtils.pauseJob(jobId);
		}
		//int count = quartzJobManager.batchUpdate(id, Constant.ScheduleStatus.PAUSE.getValue());
		return R.ok(Constant.SUCCESS);
	}
	
	@Override
	public R resume(String[] id) {
		for(String jobId : id) {
			ScheduleUtils.resumeJob(jobId);
		}
		//int count = quartzJobManager.batchUpdate(id, Constant.ScheduleStatus.NORMAL.getValue());
		return R.ok(Constant.SUCCESS);
	}

}
